import tkinter as tk
from tkinter import ttk, messagebox
import sqlite3

class StudentManagementSystem:
    def __init__(self, root):
        self.root = root
        self.root.title("学员管理系统")
        self.root.geometry("800x500")

        # 字体样式统一用 ttk.Style 设置
        style = ttk.Style()
        style.theme_use("clam")
        style.configure("TLabel", font=("微软雅黑", 10))
        style.configure("TEntry", font=("微软雅黑", 10))
        style.configure("TButton", font=("微软雅黑", 10))
        style.configure("TRadiobutton", font=("微软雅黑", 10))
        style.configure("Treeview", font=("微软雅黑", 10))
        style.configure("Treeview.Heading", font=("微软雅黑", 10, "bold"))

        # 数据库连接
        self.conn = sqlite3.connect("students.db")
        self.cursor = self.conn.cursor()
        self.create_table()

        self.setup_ui()
        self.populate_students()

    def create_table(self):
        self.cursor.execute("""
            CREATE TABLE IF NOT EXISTS students (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT,
                gender TEXT,
                age INTEGER
            )
        """)
        self.conn.commit()

    def setup_ui(self):
        form = ttk.LabelFrame(self.root, text="添加学员")
        form.pack(side="left", fill="y", padx=10, pady=10)

        ttk.Label(form, text="姓名:").grid(row=0, column=0, padx=5, pady=5, sticky="e")
        self.name_var = tk.StringVar()
        ttk.Entry(form, textvariable=self.name_var).grid(row=0, column=1, padx=5, pady=5)

        ttk.Label(form, text="性别:").grid(row=1, column=0, padx=5, pady=5, sticky="e")
        self.gender_var = tk.StringVar(value="男")
        ttk.Radiobutton(form, text="男", variable=self.gender_var, value="男").grid(row=1, column=1, sticky="w")
        ttk.Radiobutton(form, text="女", variable=self.gender_var, value="女").grid(row=1, column=1, sticky="e")

        ttk.Label(form, text="年龄:").grid(row=2, column=0, padx=5, pady=5, sticky="e")
        self.age_var = tk.StringVar()
        ttk.Entry(form, textvariable=self.age_var).grid(row=2, column=1, padx=5, pady=5)

        ttk.Button(form, text="添加", command=self.add_student).grid(row=3, column=0, columnspan=2, pady=10)

        # 表格
        self.tree = ttk.Treeview(self.root, columns=("id", "name", "gender", "age"), show="headings")
        self.tree.heading("id", text="ID")
        self.tree.heading("name", text="姓名")
        self.tree.heading("gender", text="性别")
        self.tree.heading("age", text="年龄")
        self.tree.column("id", width=50, anchor="center")
        self.tree.pack(fill="both", expand=True, padx=10, pady=10)

        # 状态栏（这里使用 tk.Label，支持 font）
        self.status_var = tk.StringVar(value="共 0 名学员")
        status = tk.Label(self.root, textvariable=self.status_var, font=("微软雅黑", 10), anchor="w")
        status.pack(fill="x")

    def populate_students(self):
        for row in self.tree.get_children():
            self.tree.delete(row)
        self.cursor.execute("SELECT * FROM students")
        rows = self.cursor.fetchall()
        for row in rows:
            self.tree.insert("", "end", values=row)
        self.status_var.set(f"共 {len(rows)} 名学员")

    def add_student(self):
        name = self.name_var.get().strip()
        gender = self.gender_var.get()
        age = self.age_var.get().strip()

        if not name or not age.isdigit():
            messagebox.showerror("错误", "请输入有效的姓名和年龄")
            return

        self.cursor.execute(
            "INSERT INTO students (name, gender, age) VALUES (?, ?, ?)",
            (name, gender, int(age))
        )
        self.conn.commit()
        self.populate_students()
        self.name_var.set("")
        self.age_var.set("")

if __name__ == "__main__":
    root = tk.Tk()
    app = StudentManagementSystem(root)
    root.mainloop()
